from flaskapp.util.Sqlalchemy import db

class Reserve(db.Model):
    __tablename__ = 'reserve'

    reserve_id = db.Column(db.Integer, primary_key=True)
    visitor_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    house_id = db.Column(db.Integer, db.ForeignKey('house.house_id'), nullable=False)
    landlord_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=False)
    appointment_time = db.Column(db.DateTime, nullable=False)
    status = db.Column(db.Enum('待确认', '已确认', '已取消'), default='待确认')
    created_at = db.Column(db.DateTime, default=db.func.current_timestamp())

    def __init__(self, visitor_id, house_id, landlord_id, appointment_time):
        self.visitor_id = visitor_id
        self.house_id = house_id
        self.landlord_id = landlord_id
        self.appointment_time = appointment_time

    def to_dict(self):
        return {
            'reserve_id': self.reserve_id,
            'visitor_id': self.visitor_id,
            'house_id': self.house_id,
            'landlord_id': self.landlord_id,
            'appointment_time': self.appointment_time.isoformat(),
            'status': self.status,
            'created_at': self.created_at.isoformat() if self.created_at else None
        }